package com.qfedu.knowledge.config;

import com.qfedu.knowledge.entity.vo.ResultVO;
import com.qfedu.knowledge.exception.BusinessRuntimeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;

/**
 * 全局异常处理器
 *
 * @ClassName GlobalExceptionHandleConfig
 * @Author ykx
 * @Date 2024/1/5 9:23
 * @Version 1.0
 */
@ControllerAdvice
@RestController
@Slf4j
public class GlobalExceptionHandleConfig {

    @ExceptionHandler(value = ArithmeticException.class)
    public ResultVO arithmeticExceptionErrorHandle(ArithmeticException e) {
        log.error(e.getMessage());
        return ResultVO.failed("算数运算异常");
    }

    @ExceptionHandler(value = SQLException.class)
    public ResultVO sqlExceptionErrorHandle(SQLException e) {
        log.info(e.getMessage());
        return ResultVO.failed("数据库异常");
    }

    @ExceptionHandler(value = BusinessRuntimeException.class)
    public ResultVO BusinessRuntimeExceptionErrorHandle(BusinessRuntimeException e) {
        log.info(e.getMessage());
        return ResultVO.failed(e.getMsg());
    }

}
